import tkinter as tk
import tkinter.messagebox as tms

import customtkinter as ctk

from Attriabte import Color, RandomPolicy
from Entiy.Config import Config
from Entiy.WordGroup import WordGroup
from Entiy.WordToValidate import WordToValidate
from View.WindowComponent.BodyFrame.BodyFrame import BodyFrame
from View.WindowComponent.BodyFrame.EditWord import EditWord
from View.WindowComponent.Layout.LayoutSide import LayoutSide


class RememberWord(BodyFrame):
    def __init__(
            self,
            layout: LayoutSide,
            wordgroup_name=None,
            with_scoller=False,
    ):
        self.frist_use = True
        self.group_name = wordgroup_name
        self.list_english = WordGroup().get_group_with_chinese(self.group_name)
        self.index = 0
        super().__init__(layout, with_scoller)
        self.list_english = RandomPolicy.loadByConfig(config=Config(), c_e=self.list_english)  # 过滤
        self.myKey = [None for i in self.list_english]

    def put_value(self, method):
        """
        放置英语与中文
        :param method: up上一页 down下一页
        :return:
        """
        print(self.list_english)

        try:
            anway = self.chinese.get()
            if anway != "":
                self.myKey[self.index] = anway
                print(self.myKey)
        except:
            ...

        if len(self.list_english) == 0:
            return
        if method != "ignore":
            self.index += 1 if method == 'down' else -1
        if self.index < 0:
            self.index = 0
            tms.showinfo("提示", '这是第一个')
        if self.index > len(self.list_english) - 1:
            self.index = len(self.list_english) - 1
            tms.showinfo("提示", '这是最后一个')

        attribate_basic = {
            "bg": Color.cutkinter
        }

        width, height = .8, .8
        inner_w = .5
        inner_h = .7

        middle_frame = ctk.CTkFrame(self.frame)

        middle_frame.place(
            relwidth=width,
            relheight=height,
            relx=(1 - width) / 2,
            rely=(1 - height) / 2
        )
        inner_frame = tk.Frame(middle_frame, bg=Color.cutkinter)
        inner_frame.place(
            relx=(1 - inner_w) / 2,
            rely=(1 - inner_h) / 2,
            relwidth=inner_w,
            relheight=inner_h,
        )

        self.up = tk.Label(
            inner_frame,
            image=self.layout.app.up_arrow,
            width=120,
            height=80,
            **attribate_basic
        )
        self.up.pack(pady=12)

        self.english = tk.Label(inner_frame, text=self.list_english[self.index][0], font=("黑体", 24),
                                **attribate_basic)
        self.english.pack(pady=6)

        self.chinese = tk.Entry(
            inner_frame,
            # placeholder_text="释义...",
            font=("黑体", 40),
            width=120
        )
        self.chinese.pack(pady=6)
        self.chinese.focus_set()
        self.chinese.bind("<Return>", lambda e: self.put_value("down"))

        ctk.CTkButton(
            middle_frame,
            text='交  卷',
            text_font=("黑体", 15),
            # command=lambda :WordToValidate(self.list_english, self.myKey)
            command=lambda: self.layout.body(
                frame=EditWord(
                    self.layout,
                    False,
                    show_dataframe=WordToValidate(
                        self.list_english,
                        self.myKey
                    ).df
                ).get(),
                must_do=True
            )
        ).pack(pady=12)

        if self.myKey[self.index] is not None:
            print("有历史")
            self.chinese.delete(0, 'end')
            self.chinese.insert(
                0, self.myKey[self.index]
            )

        self.down = tk.Label(
            inner_frame,
            image=self.layout.app.down_arrow,
            width=120,
            height=80,
            bg=Color.cutkinter
        )
        self.down.pack(pady=12)

        self.up.bind('<Button-1>', lambda e: self.put_value("up"))
        self.down.bind('<Button-1>', lambda e: self.put_value("down"))

    def pack(self):

        width, height = .8, .8

        ctk.CTkLabel(self.frame, text="  | 学习 > 测验(按回车选择下一个)", text_font=('黑体', 20), anchor=tk.W).place(
            relheight=(1 - height) / 2,
            x=0,
            y=0
        )

        self.put_value(method="ignore")

        return
